﻿package org.rkuk.kasutil.timeline 
{
	import flash.events.Event;
	import flash.utils.getTimer;
	/**
	 * 按照时间发出ClockEvent.TICK事件，发出的频率是stage.frameRate，每次发出事件时都广播以毫秒为单位的事件
	 * @author rkuk
	 */
	public class TimeClock extends FrameClock
	{
		private var _startTime:int;
		
		/**
		 * 构造函数
		 */
		public function TimeClock() 
		{
			super();
			init();
		}
		
		private function init():void
		{
			_timePast = 0;//以时刻计数时，开始时刻是0
		}
		
		/**
		 * @inheritDoc
		 */
		override public function start(immediately:Boolean=false):void 
		{
			//先记录是否已经启动了
			var isStarted:Boolean = this.isStarted;
			
			super.start(immediately);
			if(!isStarted)
				_startTime = getTimer();
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function clockTick(e:Event):void 
		{
			var tickStep:int = getTimer() - _startTime;
			_startTime += tickStep;
			_timePast += tickStep;
			_currentTimeStep = tickStep;
			
			dispatchClockEvent();
		}
	}

}